package com.example.com.transcribeeditpublish.popuWindow;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.example.com.transcribeeditpublish.R;
import com.example.com.transcribeeditpublish.activity.TCVideoRecordActivity;
import com.tencent.ugc.TXRecordCommon;

public class VoicechangerPpWindow extends PopupWindow implements View.OnClickListener {

    private AudioListener mAudioListener;
    private int mLastReverbIndex;
    private final View view;
    private int mLastVoiceChangerIndex;

    public VoicechangerPpWindow(TCVideoRecordActivity context) {
        view = LayoutInflater.from(context).inflate(R.layout.popuwindow_voice_chage, null, false);
        setContentView(view);
        setDefaultRevertAndVoiceChange();
        setListener(view);
        //适配虚拟键
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(RelativeLayout.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(RelativeLayout.LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x00000000); //android:background="#30000000"
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
    }

    private void setListener(View view) {
        view.findViewById(R.id.btn_reverb_default).setOnClickListener(this);
        view.findViewById(R.id.btn_reverb_1).setOnClickListener(this);
        view.findViewById(R.id.btn_reverb_2).setOnClickListener(this);
        view.findViewById(R.id.btn_reverb_3).setOnClickListener(this);
        view.findViewById(R.id.btn_reverb_4).setOnClickListener(this);
        view.findViewById(R.id.btn_reverb_5).setOnClickListener(this);
        view.findViewById(R.id.btn_reverb_6).setOnClickListener(this);
        view.findViewById(R.id.btn_voicechanger_default).setOnClickListener(this);
        view.findViewById(R.id.btn_voicechanger_1).setOnClickListener(this);
        view.findViewById(R.id.btn_voicechanger_2).setOnClickListener(this);
        view.findViewById(R.id.btn_voicechanger_3).setOnClickListener(this);
        view.findViewById(R.id.btn_voicechanger_4).setOnClickListener(this);
        view.findViewById(R.id.btn_voicechanger_6).setOnClickListener(this);
        view.findViewById(R.id.btn_voicechanger_7).setOnClickListener(this);
        view.findViewById(R.id.btn_voicechanger_8).setOnClickListener(this);
        view.findViewById(R.id.btn_voicechanger_9).setOnClickListener(this);
        view.findViewById(R.id.btn_voicechanger_10).setOnClickListener(this);
        view.findViewById(R.id.btn_voicechanger_11).setOnClickListener(this);


    }

    private void setDefaultRevertAndVoiceChange() {
        view.findViewById(R.id.btn_reverb_default).setBackgroundDrawable(view.getResources().getDrawable(R.drawable.round_button_3));
        mLastReverbIndex = R.id.btn_reverb_default;
        view.findViewById(R.id.btn_voicechanger_default).setBackgroundDrawable(view.getResources().getDrawable(R.drawable.round_button_3));
        mLastVoiceChangerIndex = R.id.btn_voicechanger_default;
    }

    public void show(Activity context) {
        this.showAtLocation(context.getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
    }

    public void setAudioListener(AudioListener audioListener) {
        mAudioListener = audioListener;
    }

    public interface AudioListener {
        void onSetReverb(int reverbType);

        void onSetVoiceChangerType(int voiceChangeType);

    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.btn_reverb_default) {
            if (mAudioListener != null) {
                mAudioListener.onSetReverb(TXRecordCommon.VIDOE_REVERB_TYPE_0);
            }

        } else if (i == R.id.btn_reverb_1) {
            if (mAudioListener != null) {
                mAudioListener.onSetReverb(TXRecordCommon.VIDOE_REVERB_TYPE_1);
            }

        } else if (i == R.id.btn_reverb_2) {
            if (mAudioListener != null) {
                mAudioListener.onSetReverb(TXRecordCommon.VIDOE_REVERB_TYPE_2);
            }

        } else if (i == R.id.btn_reverb_3) {
            if (mAudioListener != null) {
                mAudioListener.onSetReverb(TXRecordCommon.VIDOE_REVERB_TYPE_3);
            }

        } else if (i == R.id.btn_reverb_4) {
            if (mAudioListener != null) {
                mAudioListener.onSetReverb(TXRecordCommon.VIDOE_REVERB_TYPE_4);
            }

        } else if (i == R.id.btn_reverb_5) {
            if (mAudioListener != null) {
                mAudioListener.onSetReverb(TXRecordCommon.VIDOE_REVERB_TYPE_5);
            }

        } else if (i == R.id.btn_reverb_6) {
            if (mAudioListener != null) {
                mAudioListener.onSetReverb(TXRecordCommon.VIDOE_REVERB_TYPE_6);
            }

        } else if (i == R.id.btn_voicechanger_default) {
            if (mAudioListener != null) {
                mAudioListener.onSetVoiceChangerType(TXRecordCommon.VIDOE_VOICECHANGER_TYPE_0);
            }

        } else if (i == R.id.btn_voicechanger_1) {
            if (mAudioListener != null) {
                mAudioListener.onSetVoiceChangerType(TXRecordCommon.VIDOE_VOICECHANGER_TYPE_1);
            }

        } else if (i == R.id.btn_voicechanger_2) {
            if (mAudioListener != null) {
                mAudioListener.onSetVoiceChangerType(TXRecordCommon.VIDOE_VOICECHANGER_TYPE_2);
            }

        } else if (i == R.id.btn_voicechanger_3) {
            if (mAudioListener != null) {
                mAudioListener.onSetVoiceChangerType(TXRecordCommon.VIDOE_VOICECHANGER_TYPE_3);
            }

        } else if (i == R.id.btn_voicechanger_4) {
            if (mAudioListener != null) {
                mAudioListener.onSetVoiceChangerType(TXRecordCommon.VIDOE_VOICECHANGER_TYPE_4);
            }

        } else if (i == R.id.btn_voicechanger_6) {
            if (mAudioListener != null) {
                mAudioListener.onSetVoiceChangerType(TXRecordCommon.VIDOE_VOICECHANGER_TYPE_6);
            }

        } else if (i == R.id.btn_voicechanger_7) {
            if (mAudioListener != null) {
                mAudioListener.onSetVoiceChangerType(TXRecordCommon.VIDOE_VOICECHANGER_TYPE_7);
            }

        } else if (i == R.id.btn_voicechanger_8) {
            if (mAudioListener != null) {
                mAudioListener.onSetVoiceChangerType(TXRecordCommon.VIDOE_VOICECHANGER_TYPE_8);
            }

        } else if (i == R.id.btn_voicechanger_9) {
            if (mAudioListener != null) {
                mAudioListener.onSetVoiceChangerType(TXRecordCommon.VIDOE_VOICECHANGER_TYPE_9);
            }

        } else if (i == R.id.btn_voicechanger_10) {
            if (mAudioListener != null) {
                mAudioListener.onSetVoiceChangerType(TXRecordCommon.VIDOE_VOICECHANGER_TYPE_10);
            }

        } else if (i == R.id.btn_voicechanger_11) {
            if (mAudioListener != null) {
                mAudioListener.onSetVoiceChangerType(TXRecordCommon.VIDOE_VOICECHANGER_TYPE_11);
            }
        }

        if (v.getId() != mLastReverbIndex &&
                (v.getId() == R.id.btn_reverb_default || v.getId() == R.id.btn_reverb_1 ||
                        v.getId() == R.id.btn_reverb_2 || v.getId() == R.id.btn_reverb_3 ||
                        v.getId() == R.id.btn_reverb_4 || v.getId() == R.id.btn_reverb_5 ||
                        v.getId() == R.id.btn_reverb_6)) {
            // 混响
            v.setBackgroundDrawable(view.getResources().getDrawable(R.drawable.round_button_3));
            View lastV = view.findViewById(mLastReverbIndex);
            if (null != lastV) {
                lastV.setBackgroundDrawable(view.getResources().getDrawable(R.drawable.round_button_2));
            }
            mLastReverbIndex = v.getId();

        } else if (v.getId() != mLastVoiceChangerIndex) {
            // 变声
            v.setBackgroundDrawable(view.getResources().getDrawable(R.drawable.round_button_3));
            View lastV = view.findViewById(mLastVoiceChangerIndex);
            if (null != lastV) {
                lastV.setBackgroundDrawable(view.getResources().getDrawable(R.drawable.round_button_2));
            }
            mLastVoiceChangerIndex = v.getId();
        }
    }
}
